<!DOCTYPE html>





<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 3.7.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=7.4.0">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=7.4.0">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=7.4.0">
  <link rel="mask-icon" href="/images/logo.svg?v=7.4.0" color="#222">

<link rel="stylesheet" href="/css/main.css?v=7.4.0">


<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css?v=4.7.0">


<script id="hexo-configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '7.4.0',
    exturl: false,
    sidebar: {"position":"left","display":"post","offset":12,"onmobile":false},
    copycode: {"enable":false,"show_result":false,"style":null},
    back2top: {"enable":true,"sidebar":false,"scrollpercent":false},
    bookmark: {"enable":false,"color":"#222","save":"auto"},
    fancybox: false,
    mediumzoom: false,
    lazyload: false,
    pangu: false,
    algolia: {
      appID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    },
    localsearch: {"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},
    path: '',
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    translation: {
      copy_button: '复制',
      copy_success: '复制成功',
      copy_failure: '复制失败'
    },
    sidebarPadding: 40
  };
</script>

  <meta name="description" content="Java中获取时间日期主要涉及到这几个类：  Date —— 基本的时间对象 SimpleDateFormat —— 根据Date对象返回所需要的时间格式 Calendar —— 获取具体的日期字段">
<meta name="keywords" content="java">
<meta property="og:type" content="article">
<meta property="og:title" content="Java中获取日期及相应字段">
<meta property="og:url" content="https://ricky9090.github.io/2015/11/19/java_calendar/index.html">
<meta property="og:site_name" content="Ricky&#39;s Blog">
<meta property="og:description" content="Java中获取时间日期主要涉及到这几个类：  Date —— 基本的时间对象 SimpleDateFormat —— 根据Date对象返回所需要的时间格式 Calendar —— 获取具体的日期字段">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2018-06-11T08:46:44.015Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Java中获取日期及相应字段">
<meta name="twitter:description" content="Java中获取时间日期主要涉及到这几个类：  Date —— 基本的时间对象 SimpleDateFormat —— 根据Date对象返回所需要的时间格式 Calendar —— 获取具体的日期字段">
  <link rel="canonical" href="https://ricky9090.github.io/2015/11/19/java_calendar/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome: false,
    isPost: true,
    isPage: false,
    isArchive: false
  };
</script>

  <title>Java中获取日期及相应字段 | Ricky's Blog</title>
  








  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .logo,
  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-CN">
  <div class="container use-motion">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-meta">

    <div>
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Ricky's Blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
        <p class="site-subtitle">A long way to go.</p>
      
  </div>

  <div class="site-nav-toggle">
    <button aria-label="切换导航栏">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>


<nav class="site-nav">
  
  <ul id="menu" class="menu">
      
      
      
        
        <li class="menu-item menu-item-home">
      
    

    <a href="/" rel="section"><i class="fa fa-fw fa-home"></i>首页</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-about">
      
    

    <a href="/about/" rel="section"><i class="fa fa-fw fa-user"></i>关于</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-tags">
      
    

    <a href="/tags/" rel="section"><i class="fa fa-fw fa-tags"></i>标签</a>

  </li>
      
      
      
        
        <li class="menu-item menu-item-archives">
      
    

    <a href="/archives/" rel="section"><i class="fa fa-fw fa-archive"></i>归档</a>

  </li>
  </ul>

</nav>
</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
            

          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
      <article itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block post">
    <link itemprop="mainEntityOfPage" href="https://ricky9090.github.io/2015/11/19/java_calendar/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Ricky">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Ricky's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">Java中获取日期及相应字段

          
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              
                
              

              <time title="创建时间：2015-11-19 17:21:11" itemprop="dateCreated datePublished" datetime="2015-11-19T17:21:11+08:00">2015-11-19</time>
            </span>
          
            

            
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2018-06-11 16:46:44" itemprop="dateModified" datetime="2018-06-11T16:46:44+08:00">2018-06-11</time>
              </span>
            
          

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>Java中获取时间日期主要涉及到这几个类：</p>
<ul>
<li><code>Date</code> —— 基本的时间对象</li>
<li><code>SimpleDateFormat</code> —— 根据Date对象返回所需要的时间格式</li>
<li><code>Calendar</code> —— 获取具体的日期字段<br><br></li>
</ul>
<a id="more"></a>
<p><strong>获取某一格式的时间</strong><br>例如我们想得到 “2015-11-19”，或 “2015-11-19 12:30:00”<br>这样的字符串，可以使用Date与SimpleDateFormat来获取。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">long</span> time = System.currentTimeMillis();</span><br><span class="line">Date date = <span class="keyword">new</span> Date(time);    <span class="comment">// 生成Date对象</span></span><br><span class="line"></span><br><span class="line">SimpleDateFormat mFormat = <span class="keyword">new</span> SimpleDateFormat(<span class="string">"yyyy-MM-dd HH:mm:ss"</span>);</span><br><span class="line"></span><br><span class="line">System.out.println(mFormat.format(date));    <span class="comment">// 传入date并根据给定的格式返回相应字符串</span></span><br></pre></td></tr></table></figure>
<p>运行的结果：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">2015-11-19 17:20:13</span><br></pre></td></tr></table></figure></p>
<p><br></p>
<p>SimpleDateFormat支持的格式有很多，Javadoc中列举的有这些：</p>
<table>
<thead>
<tr>
<th style="text-align:left">格式</th>
<th style="text-align:left">样例</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">“yyyy.MM.dd G ‘at’ HH:mm:ss z”</td>
<td style="text-align:left">2001.07.04 AD at 12:08:56 PDT</td>
</tr>
<tr>
<td style="text-align:left">“EEE, MMM d, ‘’yy”</td>
<td style="text-align:left">Wed, Jul 4, ‘01</td>
</tr>
<tr>
<td style="text-align:left">“h:mm a”</td>
<td style="text-align:left">12:08 PM</td>
</tr>
<tr>
<td style="text-align:left">“hh ‘o’’clock’ a, zzzz”</td>
<td style="text-align:left">12 o’clock PM, Pacific Daylight Time</td>
</tr>
<tr>
<td style="text-align:left">“K:mm a, z”</td>
<td style="text-align:left">0:08 PM, PDT</td>
</tr>
<tr>
<td style="text-align:left">“yyyyy.MMMMM.dd GGG hh:mm aaa”</td>
<td style="text-align:left">02001.July.04 AD 12:08 PM</td>
</tr>
<tr>
<td style="text-align:left">“EEE, d MMM yyyy HH:mm:ss Z”</td>
<td style="text-align:left">Wed, 4 Jul 2001 12:08:56 -0700</td>
</tr>
<tr>
<td style="text-align:left">“yyMMddHHmmssZ”</td>
<td style="text-align:left">010704120856-0700</td>
</tr>
<tr>
<td style="text-align:left">“yyyy-MM-dd’T’HH:mm:ss.SSSZ”</td>
<td style="text-align:left">2001-07-04T12:08:56.235-0700</td>
</tr>
<tr>
<td style="text-align:left">“yyyy-MM-dd’T’HH:mm:ss.SSSXXX”</td>
<td style="text-align:left">2001-07-04T12:08:56.235-07:00</td>
</tr>
<tr>
<td style="text-align:left">“YYYY-‘W’ww-u”</td>
<td style="text-align:left">2001-W27-3</td>
</tr>
</tbody>
</table>
<p><br></p>
<p><strong>获取日期中某一字段</strong><br>有时我们需要获取诸如年、月、日这样的字段，这时就可以使用Calendar类。它的用法很简单：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">Calendar mCalendar = Calendar.getInstance();</span><br><span class="line">		</span><br><span class="line">System.out.println(mCalendar.get(Calendar.YEAR));</span><br><span class="line">System.out.println(mCalendar.get(Calendar.MONTH) + <span class="number">1</span>);    <span class="comment">// 注意月份信息的值比实际的月份数字小1</span></span><br><span class="line">System.out.println(mCalendar.get(Calendar.DAY_OF_MONTH));</span><br><span class="line">System.out.println(mCalendar.get(Calendar.HOUR_OF_DAY));</span><br><span class="line">System.out.println(mCalendar.get(Calendar.MINUTE));</span><br><span class="line">System.out.println(mCalendar.get(Calendar.SECOND));</span><br></pre></td></tr></table></figure></p>
<p>运行的结果：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">Year 2015</span><br><span class="line">Month 11</span><br><span class="line">Day 19</span><br><span class="line">Hour 17</span><br><span class="line">Minute 20</span><br><span class="line">Second 13</span><br></pre></td></tr></table></figure></p>

    </div>

    
    
    
        
      

      <footer class="post-footer">
          
            
          
          <div class="post-tags">
            
              <a href="/tags/java/" rel="tag"># java</a>
            
          </div>
        

        

          <div class="post-nav">
            <div class="post-nav-next post-nav-item">
              
                <a href="/2015/09/24/java_enum_values/" rel="next" title="Java中的枚举与values()方法">
                  <i class="fa fa-chevron-left"></i> Java中的枚举与values()方法
                </a>
              
            </div>

            <span class="post-nav-divider"></span>

            <div class="post-nav-prev post-nav-item">
              
                <a href="/2015/11/25/android_get_sha1/" rel="prev" title="Android Studio中获取SHA1">
                  Android Studio中获取SHA1 <i class="fa fa-chevron-right"></i>
                </a>
              
            </div>
          </div>
        
      </footer>
    
  </div>
  
  
  
  </article>

  </div>


          </div>
          

        </div>
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">
        
        
        
        
      

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Ricky</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        
          <a href="/archives/">
        
          <span class="site-state-item-count">54</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
    
      
      
      <div class="site-state-item site-state-tags">
        
          
            <a href="/tags/">
          
        
        <span class="site-state-item-count">10</span>
        <span class="site-state-item-name">标签</span>
        </a>
      </div>
    
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
      
      
        
      
      
        
      
        <a href="https://github.com/ricky9090" title="GitHub &rarr; https://github.com/ricky9090" rel="noopener" target="_blank"><i class="fa fa-fw fa-github"></i>GitHub</a>
      </span>
    
  </div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Ricky</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io" class="theme-link" rel="noopener" target="_blank">Hexo</a> 强力驱动 v3.7.1</div>
  <span class="post-meta-divider">|</span>
  <div class="theme-info">主题 – <a href="https://theme-next.org" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> v7.4.0</div>

        












        
      </div>
    </footer>
  </div>

  
  <script src="/lib/anime.min.js?v=3.1.0"></script>
  <script src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  <script src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="/js/utils.js?v=7.4.0"></script><script src="/js/motion.js?v=7.4.0"></script>
<script src="/js/schemes/pisces.js?v=7.4.0"></script>

<script src="/js/next-boot.js?v=7.4.0"></script>



  





















  

  

  

</body>
</html>
